﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace leviathan.enemy
{
    public class Enemy
    {
        public struct attackmode
        {
            public string name;
            public Int16 poseffect;
            public Int16 negeffect;
        }

        private string name;
        private Int16 health;
        private Int16 maxhealth;
        private Int16 mana;
        private Int16 maxmana;
        private Int16 ap;
        private Int16 dp;
        private Int16 haste;
        private Int16 xp;
        private List<attackmode> mode = new List<attackmode>();

        public Enemy(string mobname)
        {
            name = mobname;
            loadComponents();
        }

        private void loadComponents()
        {
            if (File.Exists("../../Defaults/Enemy/" + name + ".eny"))
            {
                using (StreamReader sr = new StreamReader("../../Defaults/Enemy/" + name + ".eny"))
                {
                    health = Convert.ToInt16(sr.ReadLine());
                    maxhealth = health;
                    mana = Convert.ToInt16(sr.ReadLine());
                    maxmana = mana;
                    ap = Convert.ToInt16(sr.ReadLine());
                    dp = Convert.ToInt16(sr.ReadLine());
                    haste = Convert.ToInt16(sr.ReadLine());
                    xp = Convert.ToInt16(sr.ReadLine());

                    while (!sr.EndOfStream)
                    {
                        attackmode seged;
                        seged.name = sr.ReadLine();
                        seged.poseffect = Convert.ToInt16(sr.ReadLine());
                        seged.negeffect = Convert.ToInt16(sr.ReadLine());
                        mode.Add(seged);
                    }
                    sr.Close();
                }
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Hiányzó fájl! (Enemy)", "Hiba!");
                return;
            }
        }

        public string getName()
        {
            return name;
        }

        public Int16 getHp()
        {
            return health;
        }

        public Int16 getMana()
        {
            return mana;
        }

        public Int16 getAp()
        {
            return ap;
        }

        public Int16 getDp()
        {
            return dp;
        }

        public Int16 getQuickness()
        {
            return haste;
        }

        public void setHp(Int16 a)
        {
            if (a <= 0)
            {
                health = 0;
            }
            else
            {
                health = a;
            }
        }

        public void setMana(Int16 a)
        {
            if (a <= 0)
            {
                mana = 0;
            }
            else
            {
                mana = a;
            }
        }

        public attackmode selectMode()
        {
            attackmode selected;
            int i = 0;

            if (health > maxhealth * 0.75)
            {
                int tmp = (mode.Count >= 2) ? 2 : mode.Count;
                Random rnd = new Random();
                i = rnd.Next(0, tmp);
            }
            else if(health > maxhealth*0.5)
            {
                Random rnd = new Random();
                if (mode.Count >= 4)
                {
                    i = rnd.Next(2, 4);
                }
                else
                    i = rnd.Next(0, mode.Count);
            }
            else if (health > maxhealth * 0.25)
            {
                Random rnd = new Random();
                if (mode.Count >= 6)
                {
                    i = rnd.Next(4, 6);
                }
                else
                    i = rnd.Next(0, mode.Count);
            }
            else
            {
                Random rnd = new Random();
                if (mode.Count >= 8)
                {
                    i = rnd.Next(6, 8);
                }
                else
                    i = rnd.Next(0, mode.Count);
            }

            selected = mode[i];
            if (mana - selected.negeffect < 0)
            {
                selected = mode[0];
            }
            else if (mana - selected.negeffect > maxmana)
            {
                mana = maxmana;
            }
            else
                mana = (Int16)(mana - selected.negeffect);
            return selected;
        }

        public double HpRate()
        {
            return ((double)maxhealth/health);
        }

        public Int16 getXp()
        {
            return xp;
        }
    }
}
